﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BruteForceSerializer.Contract;
using BruteForceSerializer.Contract.Internal;

namespace BruteForceSerializer.Normalization
{
    internal class NormalizedField : INormalizedField
    {
        public NormalizedField(string name, object value)
        {
            this.Name = name;
            this.Value = value;
        }

        public string Name { get; private set;}
        public object Value { get; private set;}
    }


    internal class NormalizedReference : INormalizedReference
    {
        public NormalizedReference(int objectIndex)
        {
            this.ObjectId = objectIndex;
        }

        public int ObjectId { get; private set; }
    }


    internal class NormalizedObject : INormalizedObject
    {
        private readonly IList<INormalizedField> fields;


        public NormalizedObject(Type objectType, int objectId)
        {
            this.Id = objectId;
            this.Typename = objectType.Name;
            this.fields = new List<INormalizedField>();
        }


        public void AddArrayDescription(Array array)
        {
            for(int i=0; i<array.Rank; i++)
            {
                this.Typename += i == 0 ? "=" : ",";
                this.Typename += array.GetLength(i);
            }
        }

        public void AddField(string name, object value)
        {
            this.fields.Add(new NormalizedField(name, value));
        }


        #region INormalizedObject Members

        public int Id { get; private set; }
        public string Typename { get; private set; }

        public IEnumerable<INormalizedField> Fields
        {
            get { return this.fields; }
        }

        #endregion
    }


}
